package com.xthh._function;

import android.graphics.Bitmap;
import android.graphics.Color;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class HH_ExtremeColors {

    public static float[] getExtremeColors(Bitmap image, int nClusters) {
        int width = image.getWidth();
        int height = image.getHeight();

        // 获取所有像素
        List<int[]> pixels = new ArrayList<>();
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int pixel = image.getPixel(x, y);
                int[] rgb = new int[]{
                        Color.red(pixel),
                        Color.green(pixel),
                        Color.blue(pixel)
                };
                pixels.add(rgb);
            }
        }

        // 执行K-means算法
        int[][] clusterCenters = kMeans(pixels, nClusters);

        // 找到最亮的聚类中心
        float[] brightest = new float[3];
        double maxIntensity = 0;
        for (int[] center : clusterCenters) {
            double intensity = center[0] + center[1] + center[2];
            if (intensity > maxIntensity) {
                maxIntensity = intensity;
                brightest = new float[]{center[0], center[1], center[2]};
            }
        }

        System.out.println("brightest: " + Arrays.toString(brightest));
        return brightest;
    }

    private static int[][] kMeans(List<int[]> pixels, int k) {
        // 随机选择k个初始中心
        Collections.shuffle(pixels);
        int[][] centers = new int[k][3];
        for (int i = 0; i < k; i++) {
            centers[i] = pixels.get(i);
        }

        boolean converged = false;
        while (!converged) {
            List<List<int[]>> clusters = new ArrayList<>();
            for (int i = 0; i < k; i++) {
                clusters.add(new ArrayList<>());
            }

            // 分配像素到最近的中心
            for (int[] pixel : pixels) {
                int closestCenter = 0;
                double closestDistance = Double.MAX_VALUE;
                for (int i = 0; i < k; i++) {
                    double distance = colorDistance(pixel, centers[i]);
                    if (distance < closestDistance) {
                        closestDistance = distance;
                        closestCenter = i;
                    }
                }
                clusters.get(closestCenter).add(pixel);
            }

            // 计算新的中心
            converged = true;
            for (int i = 0; i < k; i++) {
                int[] newCenter = new int[3];
                if (clusters.get(i).size() == 0) continue; // 避免空簇

                for (int[] pixel : clusters.get(i)) {
                    newCenter[0] += pixel[0];
                    newCenter[1] += pixel[1];
                    newCenter[2] += pixel[2];
                }
                newCenter[0] /= clusters.get(i).size();
                newCenter[1] /= clusters.get(i).size();
                newCenter[2] /= clusters.get(i).size();

                if (!Arrays.equals(centers[i], newCenter)) {
                    converged = false;
                }
                centers[i] = newCenter;
            }
        }

        return centers;
    }

    private static double colorDistance(int[] color1, int[] color2) {
        return Math.sqrt(Math.pow(color1[0] - color2[0], 2) +
                Math.pow(color1[1] - color2[1], 2) +
                Math.pow(color1[2] - color2[2], 2));
    }
}